<?php
class ModelAccountTransaction extends Model {
	public function getTransactions($filter = null) {
		$sql = "SELECT * FROM `@@customer_transaction` WHERE customer_id = " . (int)$this->customer->getId();

		if ($filter) {
			$sql .= $filter->sort('date_added', 'ASC');
			$sql .= $filter->limit();
		}
		else $sql .= " ORDER BY date_added";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalTransactions() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM `@@customer_transaction` WHERE customer_id = " . (int)$this->customer->getId());
	}

	public function getTotalAmount() {
		$query = $this->db->query("SELECT SUM(amount) AS total FROM `@@customer_transaction` WHERE customer_id = " . (int)$this->customer->getId() . " GROUP BY customer_id");

		if ($query->num_rows) {
			return $query->row['total'];
		} else {
			return 0;
		}
	}
}
?>